/*
 * Result.java
 *
 * Copyright(c) 2010
 * Michael Kroez
 * kroez@studiorutton.de
 *
 * All rights reserved.
 *
 * $Id$
 * 
 */

package de.studiorutton.access;

import java.text.MessageFormat;

import android.content.Context;
import de.studiorutton.gerengdic.LearningWordsApplication;
import de.studiorutton.gerengdic.R;

public class Result {
	public String msg;
	public int status;

	public final static int OKAY = 0;
	public final static int ERROR_READING_WRITING = 1;
	public final static int STATUS_REMOTE_NEWER = 2;
	public final static int STATUS_REMOTE_NOT_NEWER = 3;
	public final static int ERROR_REMOTE_ERROR = 4;
	public final static int ERROR_LOCAL_ERROR = 5;
	public final static int STATUS_CANCELLED = 6;

	private String paramOne;
	private String paramTwo;
	private ErrorCode error;

	public enum ErrorCode {
		ERROR_UNKNOWN, STATUS_DOWNLOAD_WAS_CANCELLED, ERROR_DURING_DOWNLOAD, ERROR_CANNOT_GET_EXTERNAL_STORAGE, 
		STATUS_REMOTE_FILE_NOT_NEWER, STATUS_REMOTE_FILE_NEWER, ERROR_DURING_READING_AND_WRITING, ERROR_EVERYTHING_OK_AND_DONE, 
		ERROR_CANNOT_REACH_REMOTE_SERVER
	};

	public Result(int code, String msg) {
		this.status = code;
		this.msg = msg;
	}

	public boolean isError() {
		return (status != OKAY) && ( status != STATUS_REMOTE_NEWER ) && ( status != STATUS_REMOTE_NOT_NEWER) && ( status != STATUS_CANCELLED );
	}
	
	public boolean isCancelled() {
		return ( status == STATUS_CANCELLED);
	}

	public Result(int code, ErrorCode error) {
		this.status = code;
		this.error = error;
	}

	public Result(int code, ErrorCode error, String param) {
		this.status = code;
		setErrorCodeAndParam(error, param);
	}

	public Result(int code, ErrorCode error, String param, String paramTwo) {
		this.status = code;
		setErrorCodeAndParams(error, param, paramTwo);
	}

	public void setErrorCodeAndParam(ErrorCode error, String param) {
		this.paramOne = param;
		this.error = error;
	}

	public void setErrorCodeAndParams(ErrorCode error, String paramOne,
			String paramTwo) {
		this.error = error;
		this.paramOne = paramOne;
		this.paramTwo = paramTwo;
	}

	public String getLocalizedMessage() {
		Context c = LearningWordsApplication.get();

		int id = R.string.error_unknown;
		switch (error) {
		case STATUS_DOWNLOAD_WAS_CANCELLED:
			id = R.string.error_download_was_cancelled;
			break;
		case ERROR_CANNOT_REACH_REMOTE_SERVER:
			id = R.string.error_cannot_reach_remote_server;
			break;
		case ERROR_DURING_DOWNLOAD:
			id = R.string.error_during_download;
			break;
		case ERROR_CANNOT_GET_EXTERNAL_STORAGE:
			id = R.string.error_cannot_get_external_storage;
			break;
		case STATUS_REMOTE_FILE_NOT_NEWER:
			id = R.string.error_remote_file_not_newer;
			break;
		case STATUS_REMOTE_FILE_NEWER:
			id = R.string.error_remote_file_newer;
			break;
		case ERROR_DURING_READING_AND_WRITING:
			id = R.string.error_during_reading_and_writing;
			break;
		case ERROR_EVERYTHING_OK_AND_DONE:
			id = R.string.error_everything_ok_and_done;
			break;
		default:
			id = R.string.error_unknown;
		}

		String msg = c.getResources().getString(id);
		String result = null;
		if (paramTwo != null && paramOne != null) {
			result = MessageFormat.format(msg, paramOne, paramTwo);
		} else if (paramOne != null) {
			result = MessageFormat.format(msg, paramOne);
		} else
			result = msg;

		return result;
	}
}
